// pages/updateAddress/updateAddress.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: ['广东省', '广州市', '海珠区'],
        customItem: '全部',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var id = options.id 
        var arrSplit = options.userAddress.split(',')
        var userAllAddress = this.splitArr(arrSplit)
        var arrAddress = userAllAddress[id]
        var region = arrAddress[3].split(' ')
        console.log(region)
        this.setData({
            userAddress : arrAddress,
            region:region,
            userAllAddress:userAllAddress,
            id:id,
            keyId : options.keyid
        })
        console.log(this.data.userAddress)
    },
    submit:function(e){
        console.log(e)
        var addressee = e.detail.value.addressee
        var detailAddress = e.detail.value.detailAddress
        var phoneNumber =  e.detail.value.phoneNumber
        var region = e.detail.value.region.join(' ')
        this.data.userAllAddress[this.data.id] = [addressee,detailAddress,phoneNumber,region,this.data.userAllAddress[this.data.id][4]]
        console.log(this.data.userAllAddress)
        var newUserAddress = this.getNewArr(this.data.userAllAddress)
        this.updateUserAddress(newUserAddress)
        wx.redirectTo({
          url: "/pages/addresDetail/addresDetail",
        })
    },
    getNewArr:function(e){
        var conactArr = []
        for(var i = 0 ;i<e.length;i++){
            conactArr = conactArr.concat(e[i])
        }
        return conactArr
    },
    updateUserAddress: function (e) {
        let tableName = 'userAddress'
        let recordID = this.data.keyId // 数据行 id
        
        let Product = new wx.BaaS.TableObject(tableName)
        let product = Product.getWithoutData(recordID)
        
        product.set('userAllAddress', e)
        product.update().then(res => {
          // success
          console.log('更新成功')
        }, err => {
          // err
        })
    },
    splitArr:function(arr){
        var arrSplit = []
        var tempArr = []
        var j=0
        for(var i =0;i<arr.length;i++){
                    tempArr.push(arr[i])
                    j = j+1
                if(j==5)
                    {   
                        arrSplit.push(tempArr)
                        tempArr = []
                        j=0
                    }
                }          
        return arrSplit
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})